package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }


    /**
     * 删除频道
     *
     * @param channelId
     * @return
     */
    @Override
    public ResponseResult delChannel(Integer channelId) {
        //1 检查参数
        if (channelId == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2 删除素材
        removeById(channelId);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 新增频道
     *
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult saveChannel(AdChannel adChannel) {
        //1 检查参数
        if (adChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }else {
            //2 检查是否存在频道
            LambdaQueryWrapper<WmChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            WmChannel channel = getOne(lambdaQueryWrapper.eq(WmChannel::getName, adChannel.getName()));
            if(channel != null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道已经存在");
            }else {
                //3 不存在则新增
                channel = new WmChannel();
                BeanUtils.copyProperties(adChannel,channel);
                if(channel.getIsDefault()==null){
                    channel.setIsDefault(true);
                }
                if(channel.getOrd()==null){
                    channel.setOrd(1);
                }
                if(channel.getCreatedTime()==null){
                    channel.setCreatedTime(new Date());
                }
                save(channel);
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            }
        }
    }


    /**
     * 管理端对频道分类查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findList(ChannelDto dto) {
        //1 检查参数
        if (dto == null){
            return ResponseResult.errorResult(400,"参数错误");
        }

        //2 对素材进行分页查询
        IPage page = new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<WmChannel> lambdaQueryWrapper =new LambdaQueryWrapper<>();

        //3 判断是否有关键词，有则进行关键词模糊分页查询
        if (StringUtils.isNotBlank(dto.getName())){
            lambdaQueryWrapper.like(WmChannel::getName,dto.getName());
        }

        //4 按照创建时间倒序查询
        lambdaQueryWrapper.orderByDesc(WmChannel::getCreatedTime);
        page(page,lambdaQueryWrapper);

        //结果返回
        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 修改频道
     *
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult updateChannel(AdChannel adChannel) {
        //检查参数
        if (adChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }


        //修改频道
        WmChannel wmChannel = new WmChannel();
        BeanUtils.copyProperties(adChannel,wmChannel);
        updateById(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}